/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.ExecutorDAO;
import br.com.deployment.entities.Executor;
import br.com.deployment.genericdao.GenericDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class ExecutorDAOImpl extends GenericDAO<Executor> implements ExecutorDAO {

    public ExecutorDAOImpl() {
        super(Executor.class);
    }

    @Override
    public List<Executor> findExecutor(Executor executor) {
        List<Executor> lista = new ArrayList<Executor>();

        String sql = "SELECT e FROM Executor e WHERE ";

        if ((executor.getName() != "") && (executor.getName() != null)) {
            sql += " lower(e.name) like lower(:name) and  ";
        }

        if ((executor.getOab() != "") && (executor.getOab() != null)) {
            sql += " lower(e.oab) like lower(:oab) and  ";
        }

        sql += " 1=1 ";
        System.err.println(sql);
        Query query = getEntityManager().createQuery(sql);

        if ((executor.getName() != "") && (executor.getName() != null)) {
            query.setParameter("name", "%" + executor.getName() + "%");
        }
        if ((executor.getOab() != "") && (executor.getOab() != null)) {
            query.setParameter("oab", "%" + executor.getOab() + "%");
        }

        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }

    @Override
    public List<Executor> autoCompleteExecutor(Executor executor) {
        List<Executor> lista = new ArrayList<Executor>();

        String sql = "SELECT e FROM Executor e  ";

        if ((executor.getName() != "") && (executor.getName() != null)) {
            sql += " WHERE lower(e.name) like lower(:name) ";
        }

        Query query = getEntityManager().createQuery(sql);

        if ((executor.getName() != "") && (executor.getName() != null)) {
            query.setParameter("name", "%" + executor.getName() + "%");
        }

        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }
}
